
import UIKit
import SwiftyStoreKit
import Reachability
import StoreKit
import Alamofire

@main
class AppDelegate: UIResponder, UIApplicationDelegate {


    var window: UIWindow?
    let reachability = try! Reachability()
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        //白名单
        WXNavigationBar.setup()
        WXNavigationBar.NavBar.backgroundColor = primaryBackgroundColor
        WXNavigationBar.NavBar.tintColor = .primaryColor
        
        
        if StoreManager.getUid() == 0{
            let uidString = Keychain.shared().get("uidV2")
            if let u = uidString{
                StoreManager.setUid(uid: Int(u)!)
            }else{
                let uid = Date().timeStamp
                Keychain.shared().set(uid, forKey: "uidV2")
                StoreManager.setUid(uid: Int(uid)!)
            }
        }
        
        if StoreManager.getUUIDString() == ""{
            StoreManager.setUUIDString(uuid: Keychain.shared().UUIDString)
        }
        print("uid==\(StoreManager.getUid()) uuidString=\(StoreManager.getUUIDString())")
        
        SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
            for purchase in purchases {
                switch purchase.transaction.transactionState {
                case .purchased, .restored:
                    if purchase.needsFinishTransaction {
                        // Deliver content from server, then:
                        SwiftyStoreKit.finishTransaction(purchase.transaction)
                    }
                    StoreManager.setVip(vip: true)
                    // Unlock content
                case .failed, .purchasing, .deferred:
                    break // do nothing
                }
            }
        }
        
        SKPaymentQueue.default().add(PurchaseManager.sharedInstance)
        AFManager.sharedInstance
        
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.overrideUserInterfaceStyle = .dark
//        window?.backgroundColor = .white
        window?.rootViewController =  RootViewController()
        window?.makeKeyAndVisible()
        
        
        reachability.whenReachable = { reachability in
            if reachability.connection == .wifi {
                print("Reachable via WiFi")
            } else {
                print("Reachable via Cellular")
            }
            PurchaseManager.sharedInstance.getChatGptProducts()
            PurchaseManager.sharedInstance.checkExpire()
            self.registerUid()
        }
        
        do {
            try reachability.startNotifier()
        } catch {
            print("Unable to start notifier")
        }
        
        return true
    }

   
    func registerUid(){
        if !StoreManager.isRegister(){
            let parameters = BasicParameters()
            AFManager.sharedInstance.manager.request("\(baseUrl)uid/add",method: .post,parameters: parameters,encoder: URLEncodedFormParameterEncoder.default).responseString { response in
                if let string = response.value{
                    print(string)
                    
                }else{
                    
                }
            }
            StoreManager.setRegistered()
        }
    }
    

}

